<link rel="stylesheet" href="../../layuimini/js/lay-module/step-lay/step.css" media="all">
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <div class="layui-card" id="bookCard1">
            <div class="layui-card-head">借书记录</div>
            <div class="layui-card-body">
                <form class="layui-form" lay-filter="bookForm1" style="margin: 0 auto;padding-top: 40px;">
                    <div class="layui-row layui-col-space15">
                        <div class="layui-col-xs6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">书号:</label>
                                <div class="layui-input-block">
                                    <input class="layui-form-mid layui-word-aux" type="number" style="border:none;" name="id" disabled/>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs6">
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit lay-filter="restore1">
                                        &emsp;还书&emsp;
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">书名:</label>
                        <div class="layui-input-block">
                            <input class="layui-form-mid layui-word-aux" type="text" style="border:none;" name="name" disabled/>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <div class="layui-card" id="bookCard2">
            <div class="layui-card-head">借书记录</div>
            <div class="layui-card-body">
                <form class="layui-form" lay-filter="bookForm2" style="margin: 0 auto;padding-top: 40px;">
                    <div class="layui-row layui-col-space15">
                        <div class="layui-col-xs6">
                            <div class="layui-form-item">
                                <label class="layui-form-label">书号:</label>
                                <div class="layui-input-block">
                                    <input class="layui-form-mid layui-word-aux" type="number" style="border:none;" name="id" disabled/>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs6">
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit lay-filter="restore2">
                                        &emsp;还书&emsp;
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">书名:</label>
                        <div class="layui-input-block">
                            <input class="layui-form-mid layui-word-aux" type="text" style="border:none;" name="name" disabled/>
                        </div>
                    </div>
                </form>
            </div>
        </div>

    </div>
</div>
<script>
    layui.use(['form'], function () {
        var $ = layui.$,
            form = layui.form;

        $.ajax({
            url:'/index/stu/getBorrowInfo'
            ,type: 'POST'
            ,success:function (res) {
                if(res.code == "1"){
                    const result_data = res.data;

                    if(result_data[0]){
                        form.val('bookForm1',{
                            'id':           result_data[0].id,
                            'name':         result_data[0].name
                        });
                        $("#bookCard1").show();
                    }else{
                        $("#bookCard1").hide();
                    }
                    if(result_data[1]){
                        form.val('bookForm2',{
                            'id':           result_data[1].id,
                            'name':         result_data[1].name
                        });
                        $("#bookCard2").show();
                    }else{
                        $("#bookCard2").hide();
                    }
                }else if(res.code == "-1"){
                    //layer.msg是异步方法，所以后续需要同步执行的操作放到回调函数里
                    layer.msg(res.message, {icon: 2, time: 1000},function () {
                        window.location.href = '/';
                    });
                }else{
                    layer.msg(res.message, {icon: 2});
                }
            }
        });

        form.on('submit(restore1)', function (data) {
            data.field.index = 0;
            $.ajax({
                url:'/index/stu/restoreBook'
                ,type: 'POST'
                ,data: data.field
                ,success:function (res) {
                    if(res.code == "1"){
                        layer.msg(res.message, {icon: 1, time: 1000},function () {
                            window.location.reload();
                        });
                    }else if(res.code == "-1"){
                        //layer.msg是异步方法，所以后续需要同步执行的操作放到回调函数里
                        layer.msg(res.message, {icon: 2, time: 1000},function () {
                            window.location.href = '/';
                        });
                    }else{
                        layer.msg(res.message, {icon: 2});
                    }
                }
            });
            return false;
        });

        form.on('submit(restore2)', function (data) {
            data.field.index = 1;
            $.ajax({
                url:'/index/stu/restoreBook'
                ,type: 'POST'
                ,data: data.field
                ,success:function (res) {
                    if(res.code == "1"){
                        layer.msg(res.message, {icon: 1, time: 1000},function () {
                            window.location.reload();
                        });
                    }else if(res.code == "-1"){
                        //layer.msg是异步方法，所以后续需要同步执行的操作放到回调函数里
                        layer.msg(res.message, {icon: 2, time: 1000},function () {
                            window.location.href = '/';
                        });
                    }else{
                        layer.msg(res.message, {icon: 2});
                    }
                }
            });
            return false;
        });

        form.render();
    });
</script>
